package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.mapper.QaInspectionAppearanceMapper;
import com.yupi.springbootinit.model.dto.qa.QaInspectionAppearanceAddRequest;
import com.yupi.springbootinit.model.dto.qa.QaInspectionAppearanceQueryRequest;
import com.yupi.springbootinit.model.dto.qa.QaInspectionAppearanceUpdateRequest;
import com.yupi.springbootinit.model.entity.QaInspectionAppearance;
import com.yupi.springbootinit.service.QaInspectionAppearanceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【qaInspectionAppearance(外观检验条目)】的数据库操作Service实现
* @createDate 2024-02-25 17:04:02
*/
@Service
public class QaInspectionAppearanceServiceImpl extends ServiceImpl<QaInspectionAppearanceMapper, QaInspectionAppearance>
        implements QaInspectionAppearanceService {

    @Override
    public long addQaInspectionAppearance(QaInspectionAppearanceAddRequest qaInspectionAppearanceAddRequest) {
        QaInspectionAppearance qaInspectionAppearance = new QaInspectionAppearance();
        BeanUtils.copyProperties(qaInspectionAppearanceAddRequest, qaInspectionAppearance);
        this.save(qaInspectionAppearance);
        return qaInspectionAppearance.getId();
    }

    @Override
    public boolean deleteQaInspectionAppearance(DeleteRequest deleteRequest) {
        return this.removeById(deleteRequest.getId());
    }

    @Override
    public boolean updateQaInspectionAppearance(QaInspectionAppearanceUpdateRequest qaInspectionAppearanceUpdateRequest) {
        QaInspectionAppearance qaInspectionAppearance = new QaInspectionAppearance();
        BeanUtils.copyProperties(qaInspectionAppearanceUpdateRequest, qaInspectionAppearance);
        return this.updateById(qaInspectionAppearance);
    }

    @Override
    public QaInspectionAppearance getQaInspectionAppearanceById(long id) {
        return this.getById(id);
    }

    @Override
    public List<QaInspectionAppearance> listQaInspectionAppearance(QaInspectionAppearanceQueryRequest qaInspectionAppearanceQueryRequest) {
        QueryWrapper<QaInspectionAppearance> queryWrapper = getQueryWrapper(qaInspectionAppearanceQueryRequest);
        return this.list(queryWrapper);
    }

    @Override
    public Page<QaInspectionAppearance> listQaInspectionAppearanceByPage(QaInspectionAppearanceQueryRequest qaInspectionAppearanceQueryRequest) {
        long current = qaInspectionAppearanceQueryRequest.getCurrent();
        long size = qaInspectionAppearanceQueryRequest.getPageSize();
        Page<QaInspectionAppearance> page = new Page<>(current, size);
        QueryWrapper<QaInspectionAppearance> queryWrapper = getQueryWrapper(qaInspectionAppearanceQueryRequest);
        return this.page(page, queryWrapper);
    }

    private QueryWrapper<QaInspectionAppearance> getQueryWrapper(QaInspectionAppearanceQueryRequest qaInspectionAppearanceQueryRequest) {
        QueryWrapper<QaInspectionAppearance> queryWrapper = new QueryWrapper<>();
        if (qaInspectionAppearanceQueryRequest == null) {
            return queryWrapper;
        }
        Long id = qaInspectionAppearanceQueryRequest.getId();
        if (id != null && id > 0) {
            queryWrapper.eq("id", id);
        }
        return queryWrapper;
    }
}